"""
文件的读取
    1.open(文件路径,文件操作r读,encoding=UTF-8)
    2.read()
    3.readline()
    4.readlines()
    5.with open() as f
    6.close()
"""
import time

# 文件路径变量
file_path = "/Users/dongjian/Documents/dev_file/test.txt"
# 打开文件 open()
f = open(file_path, "r", encoding="UTF-8")
print(type(f))
# 读取文件 read()
# print(f"读取10个字节的内容是：{f.read(10)}")
# print(f"读取全部的内容是:{f.read()}")

# 按行全部读取 readlines() 得到一个列表
# lines = f.readlines()
# print(type(lines))
# print(f"读取全部行的内容为:{lines}")

# readline() 一行一行的读取
line1 = f.readline()
line2 = f.readline()
line3 = f.readline()
line4 = f.readline()
print(f"第一行的数据为：{line1}")
print(f"第二行的数据为：{line2}")
print(f"第三行的数据为：{line3}")
print(f"第四行的数据为：{line4}")

# for循环读取文件行
for line in open(file_path, "r", encoding="UTF-8"):
    print(f"当前行数据为：{line}")
# 文件关闭
# time.sleep(5000000)
# f.close()

# with open 语法操作文件(会自动关闭文件)
print("----------------------------------------")
with open(file_path, "r", encoding="UTF-8") as f:
    for line in f:
        print(f"当前行内容为：{line}")
